﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2018 Jean-Marc VIGLINO,
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Timeline control</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="A control to add timeline on a map." />
  <meta name="keywords" content="ol, timeline, control, date" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/v6.15.1/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/v6.15.1/build/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>


  <link rel="stylesheet" href="../style.css" />

  <style>
    .ol-attribution {
      bottom: 4.5em;
    }
    .ol-timeline {
      font-size: 2em;
    }
    .ol-timeline .ol-scroll {
      height: 2em;
    }
    .ol-timeline .ol-features {
      text-align: center;
    }
    #select {
      font-size: .85em;
      margin: 1em 0;
    }
    #select p {
      margin: 0;
    }
    #select .copy {
      font-size: .9em;
      color: #396;
    }
    button.go:before {
      content: '';
      position: absolute;
      width: 0;
      height: .8em;
      background: transparent;
      border: 0.4em solid transparent;
      border-right: 0;
      border-left: .6em solid #fff;
      left: 50%;
      right: 50%;
      transform: translate(-50%, -50%);
      box-sizing: border-box;
    }
    .running button.go:before {
      background: transparent;
      width: .2em;
      border: 0;
      box-shadow: .2em 0, -.2em 0;
    }
    .fullscreen #map {
      position: fixed;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      width: auto!important;
      height: auto!important;
      margin: 0;
    }
    .fullscreen .options {
      position: absolute;
      top: 0;
      left: 50%;
      transform: translateX(-50%);
      margin: 0;
      padding: 0 .5em;
      font-size: 2em;
      background: rgba(255,255,255,.7);
      border-radius: 0 0 1em 1em;
    }
    .ol-timeline {
      font-size: 1.5em;
    }
    .ol-timeline .ol-scroll {
      height: 1.4em;
    }
    .img {
      position: fixed;
      right: 0;
      bottom: 0;
      display: none;
      max-width: 11em;
      max-height: 15em;
      border: .5em solid #fff;
    }
    .fullscreen .img {
      display: block;
    }
  </style>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Timeline control</h1>
  </a>
  <div class="info">
    <p>
      This example use a <i>ol/control/Timeline</i>.
      Drag and drop a file with a date attribute.
      <button onclick="$('body').toggleClass('fullscreen'); map.updateSize();">fullscreen</button>
    </p>
    <ul>
    </ul>
    <p>
      <a href="./map.control.timeline.html">More info and parameters</a>
    </p>
  </div>

  <!-- Map div -->
  <div id="map" style="width:600px; height:400px;"></div>
  <img class="img" />

  <div class='options'>
    <span class='dateStart'></span>
  </div>
  
  <script type="text/javascript">

  // Two base layers
  var geop = new ol.layer.Geoportail({
    layer: 'GEOGRAPHICALGRIDSYSTEMS.PLANIGNV2',
    opacity: .8
  });

  // Style for the clusters
  var styleCache = {};
  function getStyle (feature, resolution){
    var size = feature.get('features').length;
    var style = styleCache[size];
    if (!style) {
      var color = size>25 ? "192,0,0" : size>8 ? "255,128,0" : "0,128,0";
      var radius = Math.max(8, Math.min(size*0.5, 40));
      style = styleCache[size] = new ol.style.Style({
        image: new ol.style.Circle({
          radius: radius,
          fill: new ol.style.Fill({
            color:"rgba("+color+",.7)"
          })
        }),
        text: new ol.style.Text({
          text: size.toString(),
          //font: 'bold 12px comic sans ms',
          //textBaseline: 'top',
          fill: new ol.style.Fill({
            color: '#fff'
          })
        })
      });
    }
    return style;
  }

  var vectorSource = new ol.source.Vector();

  var currentDate = new Date('1900');

  var clusterSource = new ol.source.Cluster({
    distance: 20,
    geometryFunction: function(f) {
      if (f.get('date') < currentDate) return f.getGeometry();
      else null;
    },
    source: vectorSource
  });

  var vector = new ol.layer.Vector({
    source: clusterSource,
    style: getStyle
  });

  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View ({
      zoom: 1,
      center: [0,0]
    }),
    layers: [geop, vector]
  });

  map.addControl(new ol.control.Permalink({visible: false}));

  var dnd = new ol.interaction.DropFile();
	dnd.on ('addfeatures', function(event) { 
    vectorSource.clear();  
    vectorSource.addFeatures(event.features);
    addTimeLine(event.features)
  });
  map.addControl(dnd);

  var tline, minDate, maxDate, tdate={};
  function addTimeLine (features) {
    if (tline) {
      map.removeControl(tline);
      delete tline;
    }

    minDate = new Date('2100');
    maxDate = new Date('1900');
    features.forEach(function(f) {
      var date = new Date(f.get('date'));
      f.set('date', date);
      if (f.get('doc')) tdate[date.toLocaleDateString()] = f.get('doc');
      if (date && date < minDate) minDate = date;
      if (date && date > maxDate) maxDate = date;
    });
    console.log(minDate, maxDate)

    // Create Timeline control 
    tline = new ol.control.Timeline({
      className: 'ol-pointer',
      graduation: 'day', // 'month'
      minDate: minDate,
      maxDate: new Date(maxDate.getTime() + 24*31*60*60*1000),
      getHTML: function(f){ return 2015; },
      getFeatureDate: function(f){ return f.date; },
      endFeatureDate: function(f) { return f.endDate }
    });
    map.addControl (tline);
    // Set the date when ready
    setTimeout(function(){ tline.setDate('2015'); });
    tline.addButton ({
      className: "go",
      title: "GO!",
      handleClick: function() {
        go();
      }
    });

    // Show features on scroll
    var current='';
    tline.on('scroll', function(e){
      var d = tline.roundDate(e.date, 'day')
      $('.dateStart').text(d.toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'}));
      var doc = tdate[d.toLocaleDateString()];
      if (doc && current!==doc) {
        current = doc;
        $('.img').attr('src', 'https://espacecollaboratif.ign.fr/document/image/' + doc);
      }
      // Filter features visibility
      currentDate = d;
      vectorSource.changed();
    });
  }


  // Run on the timeline
  var running = false; 
  var speed = 100;
  function go(next) {
    var date = tline.getDate();
    if (running) clearTimeout(running);
    if (!next) {
      // stop
      if (date>minDate && date<minDate && running) {
        running = false;
        tline.element.classList.remove('running');
        return;
      }
      if (date > maxDate) {
        date = minDate;
      }
    }
    if (date > maxDate) {
      tline.element.classList.remove('running');
      return;
    }
    if (date < minDate) {
      date = minDate;
    }
    // 1 day
    date = new Date(date.getTime() + 24*60*60*1000);
    tline.setDate(date, { anim:false });
    // next
    tline.element.classList.add('running');
    running = setTimeout(function() { go(true); }, speed);
  }

  </script>
  
</body>
</html>